﻿using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using System;

namespace AsNum.Common.EntLib.Validation {
    /// <summary>
    /// 
    /// </summary>
    public abstract class BaseClientAdapter {

        /// <summary>
        /// 消息模板
        /// </summary>
        public abstract string MessageTemplate {
            get;
        }

        /// <summary>
        /// 验证类型
        /// </summary>
        public abstract RuleTypes RuleType {
            get;
        }

        public abstract Dictionary<RuleKeys, string> Details {
            get;
        }

        public abstract bool Negated {
            get;
        }

        /// <summary>
        /// 子检查器
        /// </summary>
        public List<BaseClientAdapter> SubAdapters {
            get;
            protected set;
        }

        public bool IsSub {
            get;
            protected set;
        }
    }











    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseClientAdapter<T> : BaseClientAdapter where T : ValidatorData, new() {

        /// <summary>
        /// 
        /// </summary>
        public T ValidatorData {
            get;
            set;
        }

        protected Dictionary<string , Tuple<string , string>> Map {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        protected BaseClientAdapter(T data , Dictionary<string , Tuple<string , string>> map = null , bool isSub = false) {
            this.ValidatorData = data;
            this.Map = map;
            this.IsSub = isSub;
        }

        /// <summary>
        /// 
        /// </summary>
        public override string MessageTemplate {
            get {
                return this.ValidatorData.GetMessageTemplate();
            }
        }

        /// <summary>
        /// 是否取反, false 不取反, true 取反
        /// </summary>
        public override bool Negated {
            get {
                if (this.ValidatorData is ValueValidatorData) {
                    return (this.ValidatorData as ValueValidatorData).Negated;
                } else {
                    return false;
                }
            }
        }

        private Dictionary<RuleKeys, string> ruleDetails;
        public override Dictionary<RuleKeys, string> Details {
            get {
                if (this.ruleDetails == null) {
                    this.ruleDetails = new Dictionary<RuleKeys, string>();
                    this.ruleDetails.Add(RuleKeys.Msg, string.Format("'{0}'", this.MessageTemplate.JsSafeString()));
                    this.ruleDetails.Add(RuleKeys.Type, string.Format("'{0}'", this.RuleType.ToString()));
                    this.ruleDetails.Add ( RuleKeys.Tag , string.Format ( "'{0}'" , this.ValidatorData.Tag.JsSafeString ( ) ) );

                    if (this.ValidatorData is ValueValidatorData) {
                        bool negated = (this.ValidatorData as ValueValidatorData).Negated;
                        if (negated != false)//相反为 true
                            this.ruleDetails.Add(RuleKeys.Negated, negated.JsBool());
                    }
                }
                return this.ruleDetails;
            }
        }



        protected void AddRule(RuleKeys key, string value) {
            if (!this.Details.ContainsKey(key))
                this.Details.Add(key, value);
            else
                this.Details[key] = value;
        }

    }
}
